{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Python处理JSON数据：获取、添加、删除、更新\n",
    "\n",
    ">大家好，今天我将为大家带来一篇关于使用Python处理JSON数据的教程。JSON（JavaScript Object Notation）是一种轻量级的数据交换格式，易于人阅读和编写，同时也易于机器解析和生成。Python内置了json模块，可以方便地处理JSON数据。\n",
    "\n",
    "首先，我们需要导入json模块："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d61e8f2c0cbe489d"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import json"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-25T13:13:16.831814500Z",
     "start_time": "2024-03-25T13:13:16.822273500Z"
    }
   },
   "id": "2c682aac75938ef4",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1. 获取JSON数据\n",
    "假设我们有一个JSON文件`deta.json`，内容如下："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "26122cf1e49400c1"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "{\n",
    "    \"name\": \"张三\",\n",
    "    \"age\": 30,\n",
    "    \"city\": \"北京\"\n",
    "}"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b720095c1c9291c8"
  },
  {
   "cell_type": "markdown",
   "source": [
    "我们可以使用`json.load()`函数来读取JSON文件："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f2b9d2c2151ce285"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名： 张三\n",
      "年龄： 30\n",
      "城市： 北京\n"
     ]
    }
   ],
   "source": [
    "# 打开JSON文件\n",
    "with open('deta.json', 'r', encoding='utf-8') as f:\n",
    "    data = json.load(f)\n",
    "# 输出获取到的数据\n",
    "print(\"姓名：\", data[\"name\"])\n",
    "print(\"年龄：\", data[\"age\"])\n",
    "print(\"城市：\", data[\"city\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-25T13:13:50.974230100Z",
     "start_time": "2024-03-25T13:13:50.901975700Z"
    }
   },
   "id": "88c6b2e03f9d0157",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2. 添加JSON数据\n",
    "我们可以向JSON数据中添加新的键值对："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "93a32a9a78026b99"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加后的数据： {'name': '张三', 'age': 30, 'city': '北京', 'email': 'zhangsan@example.com'}\n"
     ]
    }
   ],
   "source": [
    "# 添加新的键值对\n",
    "data[\"email\"] = \"zhangsan@example.com\"\n",
    "# 输出添加后的数据\n",
    "print(\"添加后的数据：\", data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-25T13:14:35.442484500Z",
     "start_time": "2024-03-25T13:14:35.375413900Z"
    }
   },
   "id": "ebe0079e2908199",
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3. 删除JSON数据\n",
    "我们可以使用`del`关键字来删除JSON数据中的键值对："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a736f4cec748805e"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除后的数据： {'name': '张三', 'city': '北京', 'email': 'zhangsan@example.com'}\n"
     ]
    }
   ],
   "source": [
    "# 删除键值对\n",
    "del data[\"age\"]\n",
    "# 输出删除后的数据\n",
    "print(\"删除后的数据：\", data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-25T13:15:24.264171300Z",
     "start_time": "2024-03-25T13:15:24.197443500Z"
    }
   },
   "id": "247a059544f3742f",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4. 更新JSON数据\n",
    "我们可以直接修改JSON数据中的键值对："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "46c2330c5bf7bdce"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新后的数据： {'name': '张三', 'city': '上海', 'email': 'zhangsan@example.com'}\n"
     ]
    }
   ],
   "source": [
    "# 更新键值对\n",
    "data[\"city\"] = \"上海\"\n",
    "# 输出更新后的数据\n",
    "print(\"更新后的数据：\", data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-25T13:15:56.512669700Z",
     "start_time": "2024-03-25T13:15:56.452526200Z"
    }
   },
   "id": "644e3433e7089ac3",
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5. 将JSON数据写入文件\n",
    "最后，我们可以将修改后的JSON数据写入文件："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "efb97a7afcc2a0f1"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已写入data_updated.json文件。\n"
     ]
    }
   ],
   "source": [
    "# 将数据写入JSON文件\n",
    "with open('deta.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(data, f, ensure_ascii=False, indent=4)\n",
    "print(\"数据已写入data.json文件。\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-25T13:16:54.393266300Z",
     "start_time": "2024-03-25T13:16:54.322493Z"
    }
   },
   "id": "42f48fd5576b34dc",
   "execution_count": 8
  },
  {
   "cell_type": "markdown",
   "source": [
    "这样，我们就完成了使用Python处理JSON数据的获取、添加、删除和更新操作。希望这篇教程对大家有所帮助！"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "244441a12fc0c0bd"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
